package com.learn.springmvc.interceptor;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class LoginInterceptor implements HandlerInterceptor {

	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
	}

	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
	}

	public boolean preHandle(HttpServletRequest request, HttpServletResponse arg1, Object arg2) throws Exception {
		Map params = request.getParameterMap();

		if (!params.containsKey("username")) {
			System.out.println("用户名不存在");
			return false;
		}

		String[] names = (String[]) (String[]) params.get("username");

		if ((names == null) || (names.length < 1)) {
			System.out.println("用户名不存在");
			return false;
		}
		String name = names[0];
		if (name == null || name.trim().length() == 0) {
			System.out.println("用户名为空");
			return false;
		}
		System.out.println("拦截器获取的名字为:" + name);
		return true;
	}

}
